package top.xchch.tool.resttemplate.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.xchch.tool.resttemplate.entity.Poetry;

import java.util.List;

/**
 * @author sai.zhang
 * @date 2021-09-12 11:09
 */
@Slf4j
@RestController
public class PostMappingController {

    @PostMapping("/find")
    public Poetry find(@RequestParam("id") List<Long> ids, String author) {
        // ids: [11, 12], author: xx
        log.info("ids: {}, author: {}", ids, author);
        Poetry poetry = createPoetry();
        poetry.setId(ids.get(ids.size() - 1));
        poetry.setAuthor(author);

        return poetry;
    }

    @PostMapping("/find02")
    public Poetry find02(@RequestBody Poetry poe) {
        // poe: {"author":"xx","id":0,"title":"西塞山2首"}
        log.info("poe: {}", JSON.toJSONString(poe));
        Poetry poetry = createPoetry();
        poetry.setId(poe.getId());
        poetry.setAuthor(poe.getAuthor());
        poetry.setTitle(poe.getTitle());

        return poetry;
    }

    public Poetry createPoetry() {
        Poetry poetry = new Poetry();
        poetry.setId(666106231640L);
        poetry.setTitle("西塞山二首");
        poetry.setAuthor("王周");
        poetry.setContent("西塞名山立翠屏，浓岚横入半江青。\n千寻铁锁无由问，石壁空存道者形。\n匹妇顽然莫问因，匹夫何去望千春。\n翻思岵屺传诗什，举世曾无化石人。");
        return poetry;
    }
}
